package com.example.Lezedata.ui.activity.BankCard;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.Lezedata.R;

import bid.Bid;

public class BankCardVcCell extends FrameLayout {

    private TextView name;
    private TextView card;
    private TextView default_selected;
    private TextView delete;

    public Bid.BankCardInfo cardInfo;

    public interface ClassListener{
        void delete(Bid.BankCardInfo cardInfo);
        void defaultSelect(Bid.BankCardInfo cardInfo);
    }
    public ClassListener listener;

    public static BankCardVcCell newInstance(Context context) {
        BankCardVcCell fragment = (BankCardVcCell) LayoutInflater.from(context).inflate(R.layout.bank_card_cell,null,false);
        return fragment;
    }

    public BankCardVcCell(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        findView();
    }

    void findView() {
        name = findViewWithTag("name");
        card = findViewWithTag("card");
        default_selected = findViewWithTag("default_selected");
        delete = findViewWithTag("delete");
        delete.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null){
                    listener.delete(cardInfo);
                }
            }
        });

        default_selected.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
//                if (cardInfo.getDefaultCard() == Bid.SetDefault.SD_BY_DEFAULT){
//                    return;
//                }
                if (listener != null){
                    listener.defaultSelect(cardInfo);
                }
            }
        });
    }

    public void setCardInfo(Bid.BankCardInfo cardInfo) {
        this.cardInfo = cardInfo;
        name.setText(cardInfo.getBank());
        card.setText(String.format("**** **** **** %s",cardInfo.getCardNoLast4()));
        int rid = R.drawable.login_checkbox_default;
//        switch (cardInfo.getDefaultCard()){
//            case SD_NONE:
//                rid = R.drawable.login_checkbox_default;
//                break;
//            case SD_BY_DEFAULT:
//                rid = R.drawable.login_checkbox_selected;
//                break;
//        }
        default_selected.setCompoundDrawablesWithIntrinsicBounds(rid,0,0,0);
    }
}
